import express from 'express';
import { dataManagementController } from '../controllers/dataManagementController';
import { roleAuth } from '../middleware/roleAuth';
import { Role } from '@prisma/client';
import { uploadMiddleware } from '../middleware/uploadMiddleware';
import { validateRequest } from '../middleware/validation';
import { z } from 'zod';

const router = express.Router();

// 数据导入验证schema
const importDataSchema = z.object({
  type: z.enum(['users', 'teaching-points', 'evaluations'], {
    errorMap: () => ({ message: '不支持的数据类型' })
  })
});

// 数据恢复验证schema
const restoreDatabaseSchema = z.object({
  backupFile: z.string().min(1, '备份文件名不能为空')
    .regex(/^[\w-]+\.sql$/, '无效的备份文件名')
});

// 数据导出路由
router.get('/export',
  roleAuth([Role.ADMIN]),
  validateRequest(z.object({
    type: z.enum(['users', 'teaching-points', 'evaluations'])
  })),
  dataManagementController.exportData
);

// 数据导入路由
router.post('/import',
  roleAuth([Role.ADMIN]),
  uploadMiddleware.data,
  validateRequest(importDataSchema),
  dataManagementController.importData
);

// 数据库备份路由
router.post('/backup',
  roleAuth([Role.ADMIN]),
  dataManagementController.backupDatabase
);

// 获取备份文件列表
router.get('/backup/files',
  roleAuth([Role.ADMIN]),
  dataManagementController.getBackupFiles
);

// 数据库恢复路由
router.post('/restore',
  roleAuth([Role.ADMIN]),
  validateRequest(restoreDatabaseSchema),
  dataManagementController.restoreDatabase
);

export default router;
